package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.website.dao.WebsiteNewsDao;
import com.cyt.website.model.po.WebsiteNews;
import com.cyt.website.model.vo.WebsiteNewsVo;
import com.cyt.website.service.WebsiteNewsService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网新闻资讯管理-官网新闻资讯管理Service层实现类
 *
 * @author congcong tao
 * @date 2019-8-13 10:00:52
 */
@Service("websiteNewsService")
public class WebsiteNewsServiceImpl extends BaseServiceImpl<WebsiteNews> implements WebsiteNewsService {

    @Resource
    private SysFileService sysFileService;
    @Resource
    private WebsiteNewsDao websiteNewsDao;

    @Autowired
    public void setBaseDao(WebsiteNewsDao websiteNewsDao) {
        this.baseDao = websiteNewsDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteNewsVo 对象
     * @return List 对象集合
     * @author congcong tao
     * @date 2019-8-13 10:00:52
     */
    @Override
    public List<WebsiteNews> listExample(WebsiteNewsVo websiteNewsVo) {
        Example example = new Example(WebsiteNews.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(websiteNewsVo.getName())) {
            criteria.andLike("name", "%" + websiteNewsVo.getName() + "%");
        }
        if (StrUtil.isNotEmpty(websiteNewsVo.getCode())) {
            criteria.andEqualTo("code", websiteNewsVo.getCode());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        return websiteNewsDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteNewsVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 10:00:52
     */
    @Override
    public ReturnResult page(WebsiteNewsVo websiteNewsVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteNews> list = listExample(websiteNewsVo);
        PageInfo<WebsiteNews> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteNewsVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 10:00:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteNewsVo websiteNewsVo, SysUser sysUser) {
        if (StrUtil.isEmpty(websiteNewsVo.getId())) {
            WebsiteNews websiteNews = new WebsiteNews(sysUser);
            websiteNews.setName(websiteNewsVo.getName());
            websiteNews.setPhoto(websiteNewsVo.getPhoto());
            websiteNews.setPath(getFilePath(websiteNewsVo.getPhoto()));
            websiteNews.setUrl(websiteNewsVo.getUrl());
            websiteNews.setDescription(websiteNewsVo.getDescription());
            websiteNews.setContent(websiteNewsVo.getContent());
            websiteNews.setType(websiteNewsVo.getType());
            websiteNews.setStatus(websiteNewsVo.getStatus());
            websiteNews.setCode(websiteNewsVo.getCode());
            websiteNews.setOrderNum(websiteNewsVo.getOrderNum());
            websiteNews.setSeoKeywords(websiteNewsVo.getSeoKeywords());
            websiteNews.setSeoDescription(websiteNewsVo.getSeoDescription());
            int num = websiteNewsDao.insert(websiteNews);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WebsiteNews websiteNews = websiteNewsDao.selectByPrimaryKey(websiteNewsVo.getId());
            websiteNews.setName(websiteNewsVo.getName());
            websiteNews.setPath(getFilePath(websiteNewsVo.getPhoto()));
            websiteNews.setUrl(websiteNewsVo.getUrl());
            websiteNews.setDescription(websiteNewsVo.getDescription());
            websiteNews.setContent(websiteNewsVo.getContent());
            websiteNews.setType(websiteNewsVo.getType());
            websiteNews.setStatus(websiteNewsVo.getStatus());
            websiteNews.setCode(websiteNewsVo.getCode());
            websiteNews.setOrderNum(websiteNewsVo.getOrderNum());
            websiteNews.setSeoKeywords(websiteNewsVo.getSeoKeywords());
            websiteNews.setSeoDescription(websiteNewsVo.getSeoDescription());
            websiteNews.setUpdateTime(new Date());
            websiteNews.setUpdateUser(sysUser.getId());
            int num = websiteNewsDao.updateByPrimaryKey(websiteNews);
            if (num > 0) {
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteNewsVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 10:00:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteNewsVo websiteNewsVo, SysUser sysUser) {
        //要修改的字段
        WebsiteNews websiteNews = new WebsiteNews();
        websiteNews.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        websiteNews.setUpdateUser(sysUser.getId());
        websiteNews.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteNews.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteNewsVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteNewsVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteNewsDao.updateByExampleSelective(websiteNews, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteNewsVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author congcong tao
     * @date 2019-8-13 10:00:52
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteNewsVo websiteNewsVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteNews.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteNewsVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteNewsVo.getId().split(",")));
        }
        websiteNewsDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public List<WebsiteNews> listBySize(WebsiteNewsVo websiteNewsVo, int size) {
        PageHelper.startPage(0, size);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteNews> list = listExample(websiteNewsVo);
        return list;
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }
}